//
//  BSMapCell.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/16.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "BSMapCell.h"
#import "TT_GDMapManager.h"
#import "TT_GDAnnotationView.h"


@interface BSMapCell ()<
MAMapViewDelegate,
AMapSearchDelegate,
AMapLocationManagerDelegate
>

@property (nonatomic, strong) MAMapView *mapView;

@end

@implementation BSMapCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self layout];
    }
    return self;
}

- (void)layout {
    
    [self addSubview:self.mapView];
    
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.mas_equalTo(0);
    }];
}

- (void)fillCellWithModel:(CommonSellerModule *)model {
    
    CLLocationCoordinate2D coordinate;
    coordinate.latitude = model.lat;
    coordinate.longitude = model.lng;

    MAPointAnnotation *pointAnnotation = [[MAPointAnnotation alloc] init];
    pointAnnotation.coordinate = coordinate;
    pointAnnotation.title = model.sellerName;
    pointAnnotation.subtitle = model.sellerAddress;
    pointAnnotation.lockedToScreen = NO;
    
    self.mapView.centerCoordinate = coordinate;
    [self.mapView addAnnotation:pointAnnotation];
}


#pragma mark 气泡&&大头针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation {
    

    if ([annotation isKindOfClass:[MAUserLocation class]]) {
        return nil;;
    }
    
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        TT_GDAnnotationView *annotationView = (TT_GDAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[TT_GDAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:reuseIndetifier];
        }
        annotationView.callOutView.hidden = YES;
        annotationView.image = [UIImage imageNamed:@"map_dingwei"];
        annotationView.canShowCallout = NO;
        annotationView.draggable = NO;        //设置标注可以拖动，默认为NO
        
        return annotationView;
    }
    return nil;
}

- (MAMapView *)mapView {
    if (!_mapView) {
        _mapView = [[MAMapView alloc] init];
        ///如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
        _mapView.showsUserLocation = YES;
        _mapView.userTrackingMode = 1;
        _mapView.userTrackingMode = MAUserTrackingModeFollow;
        //设置地图缩放比例，即显示区域
        [_mapView setZoomLevel:15.1 animated:YES];
        _mapView.delegate = self;
        //设置定位精度
        _mapView.desiredAccuracy = kCLLocationAccuracyBest;
        //设置定位距离
        _mapView.distanceFilter = 5.0f;
    }
    return _mapView;;
}

@end
